package com.cmict.data.bpm.controller.admin.definition;

import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormCreateReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormPageReqVO;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormRespVO;
import com.cmict.data.bpm.controller.admin.definition.vo.form.ExternalFormUpdateReqVO;
import com.cmict.data.bpm.convert.definition.ExternalFormConvert;
import com.cmict.data.bpm.dal.dataobject.definition.ExternalFormDO;
import com.cmict.data.bpm.service.definition.ExternalFormService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 工作流外部表单定义")
@RestController
@RequestMapping("/bpm/external-form")
@Validated
public class ExternalFormController {

    @Resource
    private ExternalFormService externalFormService;

    @PostMapping("/create")
    @ApiOperation("创建工作流外部表单定义")
    @PreAuthorize("@ss.hasPermission('bpm:external-form:create')")
    public CommonResult<Long> createExternalForm(@Valid @RequestBody ExternalFormCreateReqVO createReqVO) {
        return success(externalFormService.createExternalForm(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新工作流外部表单定义")
    @PreAuthorize("@ss.hasPermission('bpm:external-form:update')")
    public CommonResult<Boolean> updateExternalForm(@Valid @RequestBody ExternalFormUpdateReqVO updateReqVO) {
        externalFormService.updateExternalForm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除工作流外部表单定义")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:external-form:delete')")
    public CommonResult<Boolean> deleteExternalForm(@RequestParam("id") Long id) {
        externalFormService.deleteExternalForm(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得工作流外部表单定义")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:external-form:query')")
    public CommonResult<ExternalFormRespVO> getExternalForm(@RequestParam("id") Long id) {
        ExternalFormDO externalForm = externalFormService.getExternalForm(id);
        return success(ExternalFormConvert.INSTANCE.convert(externalForm));
    }

    @GetMapping("/list")
    @ApiOperation("获得工作流外部表单定义列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('bpm:external-form:query')")
    public CommonResult<List<ExternalFormRespVO>> getExternalFormList(@RequestParam("ids") Collection<Long> ids) {
        List<ExternalFormDO> list = externalFormService.getExternalFormList(ids);
        return success(ExternalFormConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得工作流外部表单定义分页")
    @PreAuthorize("@ss.hasPermission('bpm:external-form:query')")
    public CommonResult<PageResult<ExternalFormRespVO>> getExternalFormPage(@Valid ExternalFormPageReqVO pageVO) {
        PageResult<ExternalFormDO> pageResult = externalFormService.getExternalFormPage(pageVO);
        return success(ExternalFormConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/export-excel")
//    @ApiOperation("导出工作流外部表单定义 Excel")
//    @PreAuthorize("@ss.hasPermission('bpm:external-form:export')")
//    @OperateLog(type = EXPORT)
//    public void exportExternalFormExcel(@Valid ExternalFormExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<ExternalFormDO> list = externalFormService.getExternalFormList(exportReqVO);
//        // 导出 Excel
//        List<ExternalFormExcelVO> datas = ExternalFormConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "工作流外部表单定义.xls", "数据", ExternalFormExcelVO.class, datas);
//    }

}
